package com.sanjiahantang.ythshopping.presenter;

import android.content.Context;

import com.sanjiahantang.ythshopping.config.ConfigValue;
import com.sanjiahantang.ythshopping.http.BaseDelegate;
import com.sanjiahantang.ythshopping.http.ExceptionHelper;
import com.sanjiahantang.ythshopping.http.OkHttpClientManager;
import com.sanjiahantang.ythshopping.model.MainNearByPageModel;
import com.sanjiahantang.ythshopping.utils.Utils;
import com.sanjiahantang.ythshopping.views.MainNearByView;
import com.squareup.okhttp.Request;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by zy on 2017/8/26.
 */

public class MainNearByPresenter extends BasePresenter {


    private MainNearByView view;
    public MainNearByPresenter(MainNearByView view){
        this.view = view;
    }


    public void getMainNearByPageData(final Context context){
        initLoadDialog(context);
        if (ConfigValue.Latitude == 0 || ConfigValue.Longitude == 0){
            Utils.showToast(context, "软件没有定位权限，请到手机设置!");
        }
        mLoadingDialog.show();
        Map<String,String> para = new HashMap<>();
        String regionName = "北京";
        if(ConfigValue.currentCity != null && ConfigValue.currentCity.getRegion_name() != null){
            regionName = ConfigValue.currentCity.getRegion_name() ;
        }
        para.put("region_name", regionName);
        para.put("rid",ConfigValue.rid == null ? "2" : ConfigValue.rid);
        para.put("lat",ConfigValue.Latitude == 0 ? "39.92790482":String.valueOf(ConfigValue.Latitude));
        para.put("lng",ConfigValue.Longitude == 0 ? "116.39602661":String.valueOf(ConfigValue.Longitude));

        OkHttpClientManager.postAsyn(context, ConfigValue.APP_IP + "shop/index",para ,new BaseDelegate.ResultCallback<MainNearByPageModel>() {
            @Override
            public void onError(Request request, Object tag, Exception e) {
                dismiss();
                Utils.showToast(context, ExceptionHelper.getMessage(e, context));
            }

            @Override
            public void onResponse(MainNearByPageModel response, Object tag) {
                dismiss();
                view.getData(response);
            }
        });
    }
}
